/*------------------------------------------------------------------------
 *  Copyright (C) 2010  Luis M. de la Cruz
 *
 *  This file is part of TUNA
 *
 *  TUNA is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  TUNA is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ------------------------------------------------------------------------*/

namespace Tuna {


/*!
 ***************************************************************************
 * This function solves the systems of equations in 1D explicitly.
 ***************************************************************************
 *  \author Luis M. de la Cruz [ Mon May  4 21:31:07 CDT 2010 ]
 ***************************************************************************
 */
  template<class Teq>
  bool Solver::solExplicit(Teq &eq)
  {
    static const int bi = eq.get_bi();
    static const int ei = eq.get_ei();
    
    for(int i = bi; i <= ei; i++) {
      eq.phi(i) = eq.phi_0(i)
	- eq.aP(i) * eq.p(i) 
	+ eq.aE(i) * eq.p(i+1) 
	+ eq.aW(i) * eq.p(i-1) + eq.sp(i);
    }

    return 0;
  }

/*!
 ***************************************************************************
 * This function solves the systems of equations in 2D explicitly.
 ***************************************************************************
 *  \author Luis M. de la Cruz [ Mon May  4 21:31:07 CDT 2010 ]
 ***************************************************************************
 */
  template<class Teq>
  bool Solver::solExplicit2D(Teq &eq) 
  {
    static const int bi = eq.get_bi();
    static const int ei = eq.get_ei();
    static const int bj = eq.get_bj();
    static const int ej = eq.get_ej();
 
    for(int i = bi; i <= ei; i++) {
      for(int j = bj; j <= ej; j++) {
	eq.phi(i, j) = eq.phi_0(i, j)
	  - eq.aP(i, j) * eq.p(i, j) 
	  + eq.aE(i, j) * eq.p(i+1, j) 
	  + eq.aW(i, j) * eq.p(i-1, j) 
	  + eq.aN(i, j) * eq.p(i, j+1) 
	  + eq.aS(i, j) * eq.p(i, j-1) 
	  + eq.sp(i,j);
      }
    }

    return 0;
  }

/*!
 ***************************************************************************
 * This function solves the systems of equations in 3D explicitly.
 ***************************************************************************
 *  \author Luis M. de la Cruz [ Mon May  4 21:31:07 CDT 2010 ]
	 ***************************************************************************
 */
  template<class Teq>
  bool Solver::solExplicit3D(Teq &eq) 
  {
    static const int bi = eq.get_bi();
    static const int ei = eq.get_ei();
    static const int bj = eq.get_bj();
    static const int ej = eq.get_ej();
    static const int bk = eq.get_bk();
    static const int ek = eq.get_ek();

    for(int k = bk; k <= ek; k++) {
      for(int i = bi; i <= ei; i++) {
	for(int j = bj; j <= ej; j++) {
	  eq.phi(i, j, k) = eq.phi_0(i, j,k)
	    - eq.aP(i, j, k) * eq.p(i, j, k) 
	    + eq.aE(i, j, k) * eq.p(i+1, j, k) 
	    + eq.aW(i, j, k) * eq.p(i-1, j, k) 
	    + eq.aN(i, j, k) * eq.p(i, j+1, k) 
	    + eq.aS(i, j, k) * eq.p(i, j-1, k) 
	    + eq.aF(i, j, k) * eq.p(i, j, k+1) 
	    + eq.aB(i, j, k) * eq.p(i, j, k-1) 
	    + eq.sp(i,j,k);
	}
      }
    }

    return 0;
  }


} // Tuna namespace
